package com.syzg.base.permission.dao.impl;

import com.syzg.base.merge.model.Module;
import com.syzg.base.merge.model.Operation;
import com.syzg.base.permission.dao.PermissionDAO;
import com.syzg.common.dao.impl.BaseDAOHibernate;
import com.syzg.core.exception.ServiceException;

import java.util.*;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.syzg.base.permission.model.Permission;

@Repository("permissionDAO")
public class PermissionDAOImpl extends BaseDAOHibernate<Permission> implements PermissionDAO {

    public PermissionDAOImpl(){
    }

    protected Class<?> getEntityClass(){
        return Permission.class;
    }

    @SuppressWarnings("unchecked")
	public List<Module> findAllRoleModules(String roleId){
        String hql = "select b.operation.module from RoleOperation b where b.role.id=? order by a.orderNo";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, roleId);
        List<Module> moduleList = query.list();
        return moduleList;
    }

    @SuppressWarnings("unchecked")
	public List<Operation> findAllRoleOperations(String roleId){
        String hql = "select b.operation from RoleOperation b where b.role.id=? order by a.orderNo";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, roleId);
        List<Operation> optList = query.list();
        return optList;
    }

    @SuppressWarnings("unchecked")
	public List findAllModuleAndSelected(String roleId)
    {
        String hql = "select a,(select distinct 'checked' from RoleOperation as b where b.role.id=? and b.operation.module.id=a.id) from Module a order by a.orderNo";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, roleId);
        List moduleList = query.list();
        return moduleList;
    }

    @SuppressWarnings("unchecked")
	public List findAllOperationAndSelected(String roleId){
        String hql = "select a ,(select 'checked' from RoleOperation as b where b.role.id=? and b.operation.id=a.id) from Operation a order by a.orderNo";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, roleId);
        List optList = query.list();
        return optList;
    }

    @SuppressWarnings("unchecked")
	public boolean verifyPersonPermission(String personId, String moduleCode, String optCode){
        String hql = "select a.id from RoleOperation a where a.operation.code=? and a.operation.module.code=? and a.role.id in(select b.role.id from PersonRole b where b.person.id=?)";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, personId);
        query.setParameter(1, moduleCode);
        query.setParameter(2, optCode);
        List list = query.list();
        return (list != null && list.size() > 0);
    }

    @SuppressWarnings("unchecked")
	public List getPersonPermission(String personId){
        String hql = "select a.operation.code,a.operation.module.code from RoleOperation a where a.role.id in(select b.role.id from PersonRole b where b.person.id=?)";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, personId);
        List list = query.list();
        return list;
    }

    @SuppressWarnings("unchecked")
	public List getAllPermission()
    {
        String hql = "select a.operation.code,a.operation.module.code from RoleOperation a";
        
        Query query = this.getSession().createQuery(hql);
        List list = query.list();
        List permCodeList = new ArrayList();
        Object permCodes[];
        for(Iterator iterator = list.iterator(); iterator.hasNext(); permCodeList.add((new StringBuilder(String.valueOf((String)permCodes[0]))).append("_").append((String)permCodes[1]).toString()))
            permCodes = (Object[])iterator.next();

        return permCodeList;
    }

    @SuppressWarnings("unchecked")
	public List findModulesByPersonId(String personId) throws ServiceException
    {
        String hql = "select a from Module a where a.id in (select b.operation.module.id from RoleOperation b where b.role.id in(select c.role.id from PersonRole c where c.person.id=?)) order by a.parentModule.id,a.orderNo";
        Query query = this.getSession().createQuery(hql);
        query.setParameter(0, personId);
        List list = query.list();
        return list;
    }
}